package com.yanwq.sayhi.service;

import com.yanwq.sayhi.bean.Club;
import com.yanwq.sayhi.bean.ClubVip;
import com.yanwq.sayhi.bean.User;
import com.yanwq.sayhi.repository.ClubRepo;
import com.yanwq.sayhi.repository.ClubVipRepo;
import com.yanwq.sayhi.tool.MD5Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 作者：闫伟强。
 * 日期：2016/6/14。
 */
@Service
public class ClubVipService {

    @Autowired
    private ClubRepo clubRepo;
    @Autowired
    private ClubVipRepo repo;

    public ClubVip apply(ClubVip clubVip) {
        if (repo.countByUserAndClub(clubVip.getUser(), clubVip.getClub()) > 0) {
            return clubVip;
        }

        if (clubVip.getClub().getPermission() == 0) {
            clubVip.setPosition(4);
        } else if (clubVip.getClub().getPermission() == 1) {
            String applyPwd = clubVip.getClub().getPassword();
            String pwd = clubRepo.findOne(clubVip.getClub().getId()).getPassword();
            if (!MD5Tool.GetMD5Code(applyPwd).equals(pwd)) {
                return null;
            } else {
                clubVip.setPosition(4);
            }
        } else if (clubVip.getClub().getPermission() == 2) {
            clubVip.setPosition(0);
        }

        clubVip.setUuid(UUID.randomUUID().toString());
        clubVip.setCreateTime(System.currentTimeMillis());

        clubVip.getClub().setUuid(UUID.randomUUID().toString());
        clubVip.getClub().setCreateTime(System.currentTimeMillis());

        if (!clubRepo.exists(clubVip.getClub().getId())) {
            clubRepo.save(clubVip.getClub());
        }

        return repo.save(clubVip);
    }

    public ClubVip create(ClubVip clubVip) {
        clubVip.setPosition(2);
        clubVip.setUuid(UUID.randomUUID().toString());
        clubVip.setCreateTime(System.currentTimeMillis());

        clubVip.getClub().setUuid(UUID.randomUUID().toString());
        clubVip.getClub().setCreateTime(System.currentTimeMillis());
        clubRepo.save(clubVip.getClub());
        return repo.save(clubVip);
    }

    public ClubVip pass(ClubVip clubVip) {
        clubVip.setPosition(4);
        return repo.save(clubVip);
    }

    public ClubVip refuse(ClubVip clubVip) {
        clubVip.setPosition(1);
        return repo.save(clubVip);
    }

    public ClubVip manager(ClubVip clubVip) {
        clubVip.setPosition(3);
        return repo.save(clubVip);
    }

    public void delete(ClubVip clubVip) {
        repo.delete(clubVip);
    }

    public void remove(long userId, long clubId) {
        User user = new User();
        user.setId(userId);
        Club club = new Club();
        club.setId(clubId);
        repo.removeByUserAndClub(user, club);
    }

    public List<ClubVip> listByUser(long userId, int page, int size) {
        User user = new User();
        user.setId(userId);
        Page<ClubVip> clubVipPage = repo.findByUserOrderByIdDesc(user, new PageRequest(page, size));
        return clubVipPage.getContent();
    }

    public List<ClubVip> listByClub(long clubId, int page, int size) {
        Club club = new Club();
        club.setId(clubId);
        Page<ClubVip> clubVipPage = repo.findByClubOrderByIdDesc(club, new PageRequest(page, size));
        return clubVipPage.getContent();
    }

    public List<ClubVip> listByUserAndPosition(long userId, int position, int page, int size) {
        User user = new User();
        user.setId(userId);
        Page<ClubVip> clubVipPage = repo.findByUserAndPositionOrderByIdDesc(user, position, new PageRequest(page, size));
        return clubVipPage.getContent();
    }
}